<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/23
 * Time: 11:08
 */
namespace work{
    use app\ctr;
    use C\lib\Db;
    use C\lib\Session;
    use C\mod\address;
    use C\mod\order;
    use C\lib\Ditu;
    use C\mod\worker;
    use C\mod\uaddres;
    use C\lib\Wxpay;
    use C\lib\Wxapi;
    use C\mod\workerd;
    use C\mod\evaluate;

    class borders extends ctr{
        // --- 定义构造函数 ---
        function __construct() {
            Db::connect();
        }
        /*
         * 订单页面
         */
        public function order(){
            Session::start();
            if(isset($_SESSION['user.id'])){

                $list= [];
                $count=count(order::getList(['user_id'=>$_SESSION['user.id']]));
                $pagesize=10;
                $totalpage=ceil($count/$pagesize);
                //var_dump($count);exit;
                if($count>0){
                    if (!empty($_POST['page'])) {
                        $page = $_POST['page'] + 0;
                        if($page<0){
                            $page=1;
                        }elseif ($page>$totalpage){
                            $page=$totalpage;
                        }
                    } else {
                        $page = 1;
                    }
                    $list=order::getList(['user_id'=>$_SESSION['user.id'],'state'=>2],[($page - 1) * $pagesize, $pagesize],['id','DESC']);
                }
                $this->loadView("bdkd/order/order",['list'=>$list]);
            }
        }


        /*
         * 待处理订单页面
         */
        public function untreated(){
            Db::connect();
            Session::start();
            if(isset($_SESSION['user.id'])){
                $list=[];
                $data=order::getList(['user_id'=>$_SESSION['user.id'],['state','!=',2],['state','!=',3],['state','!=',3]],[0,1],['id','DESC']);

                //var_dump($data[0]);//exit;
                if($data){
                    if($data[0]->state==1){
                        //echo 55555;//exit;
                        if(!empty($data[0]->worker_id)){
                            $worker=worker::get(['id'=>$data[0]->worker_id]);
                           // var_dump($worker);exit;
                            $workerd=workerd::get(['worker_id'=>$data[0]->worker_id]);
                           // var_dump($workerd);
                            $list=[
                                'id'=>$data[0]->id,
                                'state'=>3,
                                'realname'=>$workerd->realname,
                                'phone'=>$workerd->phone,
                                'head_oss' => $worker->head_oss != '' ? STATIC_PATH.$worker->head_oss : '',
                                'lat'=>$data[0]->lat,//物品的经纬度
                                'lng'=>$data[0]->lng,
                                'wlat'=>$worker->lat,//快递员的经纬度
                                'wlng'=>$worker->lng,
                                'type'=>$data[0]->type,
                                'time'=>$data[0]->time_add
                            ];
                           //var_dump($list);//exit;
                        }
                       // print_r($list);
                    }else{
                        //echo 333;
                        $list=[
                            'id'=>$data[0]->id,
                            'state'=>2,
                            'type'=>$data[0]->type,
                            'time'=>$data[0]->time_add,
                            'signPackage'=>''
                        ];

                    }
                }else{
                   //echo 22;exit;
                    $list=[
                        'id'=>'',
                        'type'=>'',
                        'state'=>1,
                        'time'=>'',
                        'signPackage'=>Wxapi::getSignPackage()
                    ];
                }
               // var_dump($list);//exit;
                $this->loadView("bdkd/order/map",['list'=>$list]);
            }else{
                $this->loadView("bdkd/sign/main");
            }
        }

        /*
         * 服务评价页面
         */
        public function evaluate(){
            Session::start();
            if(isset($_SESSION['user.id'])){
                if(isset($_GET['id'])){
                    $order=order::get(['id'=>$_GET['id']]);
                    $worker=workerd::get(['worker_id'=>$order->worker_id]);
                    $user=\C\mod\user::get(['id'=>$order->user_id]);
                   // var_dump($user->id);exit;
                   // $this->loadView("bdkd/order/service",['list'=>1,'worker'=>1,'num'=>1,'zhekou'=>1]);
                    $this->loadView("bdkd/order/service",['name'=>$worker->realname,'pathT'=>$user->path,'worker_id'=>$worker->worker_id,'user_id'=>$user->id]);
                }
            }
        }
        /*
         * 添加服务评价
         */
        public function addEvaluate(){
            Session::start();
            if(isset($_SESSION['user.id'])){
                if(empty($_POST['worker_id'])){
                    $this->writeJson(0,'缺少参数1!');exit;
                }
                if(empty($_POST['user_id'])){
                    $this->writeJson(0,'缺少参数2!');exit;
                }
                if(empty($_POST['content'])){
                    $this->writeJson(0,'缺少参数3!');exit;
                }
                $evaluate=new evaluate();
                $evaluate->set([
                    'worker_id'=>$_POST['worker_id'],
                    'user_id'=>$_POST['user_id'],
                    'content'=>$_POST['content'],
                    'time_add'=>$_SERVER['REQUEST_TIME']
                ]);
                if($evaluate->create()){
                    $this->writeJson(1);
                }else{
                    $this->writeJson(0,'评论失败!');
                }
            }
        }


        /*
         * 取消订单
         */
        public function upOrder(){
            Session::start();
            Db::connect();
            if(isset($_SESSION['user.id'])){
               // echo 999;exit;
                if(empty($_POST['id'])){
                    $this->writeJson(0,'缺少订单id参数！');exit;
                }
                //$this->writeJson(0,$_POST['id']);exit;
                //var_dump($_POST);exit();
               // var_dump($_POST['id']);
                $order=order::get(['id'=>$_POST['id']]);
                if($order->state==1){
                    $this->writeJson(0,'代取员已接单取消订单失败！');exit;
                }else{
                    $order->set([
                        'state'=>3
                    ]);
                    if($order->update()){
                        $this->writeJson(1);exit;
                    }else{
                        $this->writeJson(0,'取消订单失败！');exit;
                    }
                }
            }
        }
        /*
         * 寄取记录
         */
        public function record(){
            Session::start();
            if(isset($_SESSION['user.id'])){
                $list= [];
                $count=count(order::getList(['user_id'=>$_SESSION['user.id'],'state'=>2]));
                //var_dump($count);exit;
                $pagesize=10;
                $totalpage=ceil($count/$pagesize);
                //var_dump($count);exit;
                if($count>0){
                    if (!empty($_POST['page'])) {
                        $page = $_POST['page'] + 0;
                        if($page<0){
                            $page=1;
                        }elseif ($page>$totalpage){
                            $page=$totalpage;
                        }
                    } else {
                        $page = 1;
                    }
                    $list=order::getList(['user_id'=>$_SESSION['user.id'],'state'=>2],[($page - 1) * $pagesize, $pagesize],['id','DESC']);
                }
                $this->loadView('bdkd/user/record',['list'=>$list]);
            }
        }








        /***********************************************************************************/

        /*
         * 快递地址信息添加
         */
        public function addres(){
            Session::start();
            if(isset($_SESSION['user.id'])){
                if(isset($_POST['address_sender'])){
                }else{
                    $this->writeJson(0,'请输入地址!');
                }
                if(isset($_POST['phone_sender'])){
                }else{
                    $this->writeJson(0,'请输入手机号!');
                }
                if(isset($_POST['name_sender'])){
                }else{
                    $this->writeJson(0,'请输入姓名!');
                }
                $addres=new uaddres();
                $addres->set([
                   'address_sender' => $_POST['address_sender'],
                    'phone_sender' => $_POST['phone_sender'],
                    'name_sender' => $_POST['name_sender']
                ]);
                $addres->create();
            }else
                $this->loadView('bdkd/sign/main');
        }
        /*
         * 显示用户地址信息
         */
        public function getaddres(){
            Session::start();
            if(isset($_SESSION['user.id'])){
                $data=address::getList(['user_id'=>$_SESSION['user.id']]);
                $this->writeJson(1,['data'=>$data]);
            }else
                $this->loadView('work/sgin/main');//登录页面
        }
        /*
         * 显示寄件页面
         */
        public function sOrder(){
            Session::start();
            if(isset($_SESSION['user.id'])){
                $this->loadView('寄件页面地址');
            }else
                $this->loadView('work/sgin/main');//登录页面
        }
        /*
         * 寄快递
         */
        public function sendOrder(){
            Session::start();
            if(isset($_SESSION['user.id'])){
                if(isset($_POST['address_sender'])){
                }else{
                    $this->writeJson(0,'请输入寄件人地址!');
                }
                if(isset($_POST['phone_sender'])){
                }else{
                    $this->writeJson(0,'请输入寄件人手机号!');
                }
                if(isset($_POST['name_sender'])){
                }else{
                    $this->writeJson(0,'请输入寄件人姓名!');
                }
                if(isset($_POST['address_recipient'])){
                }else{
                    $this->writeJson(0,'请输入收件人地址!');
                }
                if(isset($_POST['phone_recipient'])){
                }else{
                    $this->writeJson(0,'请输入收件人手机号!');
                }
                if(isset($_POST['name_recipient'])){
                }else{
                    $this->writeJson(0,'请输入收件人姓名!');
                }
                if(isset($_POST['goods_type'])){
                }else{
                    $this->writeJson(0,'请选择物品类型!');
                }
                if(isset($_POST['weight'])){
                }else{
                    $this->writeJson(0,'请填写物品重量');
                }
                if(isset($_POST['express_name'])){
                }else{
                    $this->writeJson(0,'请选择快递公司');
                }
                if(isset($_POST['context'])){
                    $data['context']=$_POST['context'];//填入反馈表
                }
//                事务处理
//                try {
//                    Db::beginTransaction();
//
//                    if ($dbw && $dbp) {
//                        Db::commit();
//                    } else {
//                        Db::rollBack();
//                    }
//                } catch (\Exception $e) {
//                    Db::rollBack(); // 执行失败，事务回滚
//                    $this->writeJson(0,'寄件失败请重新提交!');
//                    //$this->writeAesJson(0, '操作库失败' . $e->getMessage());
//                }
                $order=new \C\mod\order();
                $order->set([
                    'address_sender' => $_POST['address_sender'],
                    'phone_sender' => $_POST['phone_sender'],
                    'name_sender' => $_POST['name_sender'],
                    'address_recipient' => $_POST['address_recipient'],
                    'phone_recipient' => $_POST['phone_recipient'],
                    'name_recipient' => $_POST['name_recipient'],
                    'goods_type' => $_POST['goods_type'],
                    'weight' => $_POST['weight'],
                    'express_name' => $_POST['express_name'],
                    'type' => 1
                ]);
                if($order->create()){
                    $this->writeJson(1);
                }else{
                    $this->writeJson(0,'寄件失败请重新提交!');
                }
            }
        }

        /*
         * 取件
         */
        public function getOrder(){

        }





        /*
         * 获取工作人员经纬度
         */
        private function workAddres($uid){
            $find=Db::query('select w.lat,w.lng from bd_order o, bd_worker w WHERE o.worker_id = w.id AND o.worker_id='.$uid);
            $data = $find->fetch(\PDO::FETCH_ASSOC);
            return $data;
        }
        /*
         * 得到距离
         */
        public function getDistance(){
            $data=Ditu::curlGetWeb('北京市海淀区万霖大厦');
            $limit = Ditu::getDistance("39.976315","116.495602","39.976315","116.492302");
            echo $limit;
            echo print_r($data);
            exit;
            Session::start();
            if(isset($_SESSION['user.id'])){
                $arr = [];
                $arr=order::get(['user_id'=>$_SESSION['user.id']]);//得到用户的地址
                $data=Ditu::curlGetWeb($arr->address_sender);//得到用户经纬度
                $find=$this->workAddres($_SESSION['user.id']);//得到工作人员经纬度
                $distance=Ditu::getDistance($data['lat'],$data['lng'],$find['lat'],$fin['lng']);
                $this->writeJson(1,$distance);
            }else
                $this->writeJson(0,'请您登录');

        }



    }
}